﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using TwitterClient2.Controller;
using System.Drawing;
using Microsoft.Win32;

namespace TwitterClient2.Components
{
    class PanelLogin : Panel
    {
        private System.Windows.Forms.Label labelUsername;
        private System.Windows.Forms.TextBox textBoxUsername;
        private System.Windows.Forms.Label labelPassword;
        private System.Windows.Forms.Label labelLoginNotice;
        private System.Windows.Forms.Button buttonLogin;
        private System.Windows.Forms.TextBox textBoxPassword;
        private String username = "";
        private String password = "";
        private CheckBox checkBoxSave;
        private TwitterClientController controller = TwitterClientController.getInstance();
        public PanelLogin()
        {
            RegistryKey userkey = Registry.CurrentUser;
            RegistryKey softwarekey = userkey.OpenSubKey("Software", true);
            RegistryKey mainkey = softwarekey.OpenSubKey("TwitterClient", true);
            if (mainkey != null)
            {

                this.username = (string)mainkey.GetValue("user");
                this.password = (string)mainkey.GetValue("pw");
            }
            IntializeComponent();
        }


        private void IntializeComponent()
        {
            this.labelLoginNotice = new System.Windows.Forms.Label();
            this.buttonLogin = new System.Windows.Forms.Button();
            this.textBoxPassword = new System.Windows.Forms.TextBox();
            this.labelPassword = new System.Windows.Forms.Label();
            this.labelUsername = new System.Windows.Forms.Label();
            this.textBoxUsername = new System.Windows.Forms.TextBox();
            this.checkBoxSave = new System.Windows.Forms.CheckBox();

            this.BackColor = System.Drawing.Color.LightGray;
            this.Controls.Add(this.labelLoginNotice);
            this.Controls.Add(this.buttonLogin);
            this.Controls.Add(this.textBoxPassword);
            this.Controls.Add(this.labelPassword);
            this.Controls.Add(this.labelUsername);
            this.Controls.Add(this.textBoxUsername);
            this.Controls.Add(this.checkBoxSave);
            this.Name = "panelLogin";
            this.Size = new System.Drawing.Size(202, 149);
            this.Visible = false;

            // 
            // labelLoginNotice
            // 
            this.labelLoginNotice.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Regular);
            this.labelLoginNotice.Location = new System.Drawing.Point(5, 6);
            this.labelLoginNotice.Name = "labelLoginNotice";
            this.labelLoginNotice.Size = new System.Drawing.Size(193, 38);
            this.labelLoginNotice.Text = "Please enter your Username and your Password!";
            // 
            // buttonLogin
            // 
            this.buttonLogin.Location = new System.Drawing.Point(81, 129);
            this.buttonLogin.Name = "buttonLogin";
            this.buttonLogin.Size = new System.Drawing.Size(72, 20);
            this.buttonLogin.TabIndex = 4;
            this.buttonLogin.Text = "OK";
            this.buttonLogin.Click += new System.EventHandler(this.buttonLogin_Click);
            // 
            // textBoxPassword
            // 
            this.textBoxPassword.Location = new System.Drawing.Point(81, 78);
            this.textBoxPassword.Name = "textBoxPassword";
            this.textBoxPassword.PasswordChar = '*';
            this.textBoxPassword.Size = new System.Drawing.Size(100, 21);
            this.textBoxPassword.TabIndex = 3;
            this.textBoxPassword.Text = this.password;
            // 
            // labelPassword
            // 
            this.labelPassword.Location = new System.Drawing.Point(6, 81);
            this.labelPassword.Name = "labelPassword";
            this.labelPassword.Size = new System.Drawing.Size(62, 18);
            this.labelPassword.Text = "Password:";
            // 
            // labelUsername
            // 
            this.labelUsername.Location = new System.Drawing.Point(4, 48);
            this.labelUsername.Name = "labelUsername";
            this.labelUsername.Size = new System.Drawing.Size(64, 21);
            this.labelUsername.Text = "Username:";
            // 
            // textBoxUsername
            // 
            this.textBoxUsername.Location = new System.Drawing.Point(81, 48);
            this.textBoxUsername.Name = "textBoxUsername";
            this.textBoxUsername.Size = new System.Drawing.Size(100, 21);
            this.textBoxUsername.TabIndex = 0;
            this.textBoxUsername.Text = this.username;
            // checkBoxSave
            this.checkBoxSave.Location = new Point(5, 109);
            this.checkBoxSave.Size = new Size(200, 20);
            this.checkBoxSave.Text = "Save Username/Password";
            this.checkBoxSave.Checked = true;
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            this.Enabled = false;
            this.Refresh();
            TwitterClientEvent twitterEvent = new TwitterClientEvent();
            twitterEvent.paramList.Add(this.textBoxUsername.Text);
            twitterEvent.paramList.Add(this.textBoxPassword.Text);
            if (checkBoxSave.Checked == true)
                twitterEvent.paramList.Add(true);
            else
                twitterEvent.paramList.Add(false);
            this.controller.performUserInput(TwitterClientController.LOGIN_CLICK, twitterEvent);
            this.Enabled = true;
            Cursor.Current = Cursors.Default;
        }


    }

}
